using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.ComponentModel;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Timers;

namespace MouseReplay
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    class MousePos
    {
        private Double x = 0;
        private Double y = 0;

        public Double X
        {
            get { return x; }
        }

        public Double Y
        {
            get { return y; }
        }

        public MousePos(Double x, Double y)
        {
            this.x = x;
            this.y = y;
        }
    }
    public partial class Window1 : Window
    {
        List<MousePos> movimentos = new List<MousePos>();
        System.Timers.Timer tempo = new Timer();
        BackgroundWorker bkw = new BackgroundWorker();
        Boolean gravando = false;

        public Window1()
        {
            InitializeComponent();
            this.MouseMove += new MouseEventHandler(Window1_MouseMove);
            this.bkw.DoWork += new DoWorkEventHandler(bkw_DoWork);
        }

        void bkw_DoWork(object sender, DoWorkEventArgs e)
        {
            replay();
        }

        void Window1_MouseMove(object sender, MouseEventArgs e)
        {
            if (gravando)
            {
                Double X = e.GetPosition(canvas1).X;
                Double Y = e.GetPosition(canvas1).Y;
                movimentos.Add(new MousePos(X, Y));
            }
        }

        private void Record_Bt_Click(object sender, RoutedEventArgs e)
        {
            movimentos.Clear();
            canvas1.Children.Clear();
            gravando = true;
        }

        private void Stop_Bt_Click(object sender, RoutedEventArgs e)
        {
            gravando = false;
        }

        private void Replay_Bt_Click(object sender, RoutedEventArgs e)
        {
            replay();
        }

        private void replay()
        {
             for (int i = 0; i < movimentos.Count; i++)
            {
                try
                {
                    Line l = new Line();
                    l.X1 = movimentos[i].X;
                    l.Y1 = movimentos[i].Y;
                    l.X2 = movimentos[i + 1].X;
                    l.Y2 = movimentos[i + 1].Y;
                    l.Stroke = Brushes.White;
                    l.StrokeThickness = 2f;
                    canvas1.Children.Add(l);
                    System.Threading.Thread.Sleep(10);
                }
                catch
                { }
            }
        }

    }
}
